//
//  DMInfoViewController.m
//  DMGME
//
//  Created by MC on 15/10/24.
//  Copyright © 2015年 GYP. All rights reserved.
//

#import "DMInfoViewController.h"
#import "DMInFoViewCell.h"
#import "DMInfoModel.h"
@interface DMInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableView;
    NSMutableArray *_datas;
}
@end


@implementation DMInfoViewController
-(void)segmentAction:(UISegmentedControl *)Seg{
//    NSInteger Index = Seg.selectedSegmentIndex;
//    NSLog(@"Seg.selectedSegmentIndex:%ld",(long)Index);
    if (Seg.selectedSegmentIndex == 1) {
        
       
        [self requestDataForNotice];
    }
    if(Seg.selectedSegmentIndex == 0)
    {
        [_tableView removeFromSuperview];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSArray *buttonNames = [NSArray arrayWithObjects:@"消息", @"通知", nil];
    UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:buttonNames];
    segmentedControl.frame = CGRectMake(KWidth/2-45, 10, 90, 30);
    segmentedControl.selectedSegmentIndex=0;
    segmentedControl.layer.masksToBounds = YES;
    segmentedControl.layer.cornerRadius = 2;
    //添加事件
    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segmentedControl;

    _datas = [NSMutableArray array];
    _tableView = [[UITableView alloc] init];
    _tableView.frame = CGRectMake(0, 0, KWidth, KHeight-64);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[DMInFoViewCell class] forCellReuseIdentifier:@"cell"];
    [self requestDataForNotice];

}
-(void)goBackOnly
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark Delegate/DataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _datas.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 72;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *ident = @"cell";

    DMInFoViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident forIndexPath:indexPath];

    UILabel *linr = [[UILabel alloc] initWithFrame:CGRectMake(0, cell.contentView.frame.size.height-1.5, KWidth, 0.9)];
    linr.backgroundColor = [UIColor lightGrayColor];
    linr.alpha = 0.89;
    [cell.contentView addSubview:linr];

    DMInfoModel *model = _datas[indexPath.row];
    
    cell.model = model;
    
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return TRUE;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    [_datas removeObject:_datas[indexPath.row]];
    [_tableView reloadData];
}

-(void)requestDataForNotice
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:[AFDownManager MD5String:@"%^&*dongmangoug!@#$"] forKey:@"token"];
    [dict setValue:kkUserDict[@"id"] forKey:@"getId"];
    [dict setValue:[NSString stringWithFormat:@"%d",[[[NSUserDefaults standardUserDefaults] objectForKey:@"level"] intValue]+2]forKey:@"objId"];
    [self StartLoading];
    [AFDownManager POST:SystemNotice parameters:dict success:^(id responseObject) {
        
    
        
        
        [_datas removeAllObjects];
        [self StopLoading];
//        NSLog(@"responseObject == %@",responseObject);
        NSArray *arr = responseObject[@"data"][@"listData"];
        for (NSDictionary *dic in arr) {
//            NSLog(@"44==%@",responseObject);
            DMInfoModel *dmModel = [DMInfoModel modelWithDic:dic];
            [dmModel setValuesForKeysWithDictionary:dic];
            
            [_datas addObject:dmModel];
        }
        [_tableView reloadData];
        //取消下拉和上拉的状态
        [_tableView headerEndRefreshing];
        //        if (_datas.count == 0) {
        //            [self showAltreViewMessage];
        //        }
    } failure:^(NSError *error) {
    
        [self StopLoading];
//        NSLog(@"error==%@",error);
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
